package com.lantosec.hmrp.modules.internal.controller;


import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.modules.internal.entity.InternalType;
import com.lantosec.hmrp.modules.internal.vo.InternalTypeVO;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.internal.entity.InternalItem;
import com.lantosec.hmrp.modules.internal.service.IInternalItemService;
import com.lantosec.hmrp.modules.internal.vo.InternalItemVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 项目定义 控制器
 *
 * @author AutoCode
 * @since 2020-04-22
 */
@Api(value = "项目定义控制器", tags = "项目定义控制器")
@RestController
@RequestMapping("/internal/item")
@Log4j2
public class InternalItemController extends BaseController {
    @Autowired
    private IInternalItemService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<InternalItemVO>> all() {
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<List<InternalItemVO>> page(@ApiParam(value = "项目名称") @RequestParam(name = "internalItemName",required = false) String internalItemName,
                                             @ApiParam(value = "项目类型") @RequestParam(name = "internalItemType",required = false) Integer internalItemType,
                                             @ApiParam(value = "项目分类") @RequestParam(name = "internalTypeId",required = false) Integer internalTypeId){
        List<InternalItemVO> internalItemVOList = service.getAll(internalItemName,internalItemType,internalTypeId);
        return Result.ok(internalItemVOList);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<InternalItemVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @GetMapping("getInternalItem")
    @ApiOperation(value = "查询不同评价的评价项目")
    public Result<List<InternalTypeVO>> getInternalItem(@ApiParam(value = "项目类型", required = true) @RequestParam(name="internalItemType") Integer internalItemType){
        List<InternalTypeVO> internalItems = service.getInternalItem(internalItemType);
        return Result.ok(internalItems);
    }

    @PostMapping("create")
    @ControllerLog(name = "新增内部满意度评价项目", type = LogType.add)
    @ApiOperation(value = "新增内部满意度评价项目")
    public Result<String> create(@ApiParam(value = "项目定义VO对象", required = true) @RequestBody @Validated InternalItemVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        InternalItem entity = vo.convertToEntity();
        String internalItemId = service.queryMaxId();
        entity.setInternalItemId(internalItemId);
        service.save(entity);
        return Result.ok(entity.getInternalItemId());
    }

    @PutMapping("update")
    @ControllerLog(name = "修改内部满意度评价项目", type = LogType.update)
    @ApiOperation(value = "修改内部满意度评价项目")
    public Result<Void> update(@ApiParam(value = "项目定义VO对象", required = true) @RequestBody InternalItemVO vo){
        InternalItem entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "逻辑删除内部满意度评价项目", type = LogType.delete)
    @ApiOperation("逻辑删除内部满意度评价项目")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
